﻿using LayeredSkin.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

using Top_Meeting.Properties;
using Top_Meeting.Window;

namespace Top_Meeting.UI
{
    class UI_ControlBox
    {
        public Control Parent;
        bool[] buttons;

        private Size buttonSize = new Size(32, 32);

        private LayeredPanel layPanel_ControlBox;
        private LayeredButton layBtn_Mini;
        private LayeredButton layBtn_StateSwitch;
        private LayeredButton layBtn_Close;

        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frm_Login));

        public UI_ControlBox(Control parent, params bool[] buttons)
        {
            Parent = parent;
            this.buttons = buttons;

            Creat();

            UpdateLayout();
        }

        private void Creat()
        {
            //control box panel
            layPanel_ControlBox = new LayeredPanel();
            layPanel_ControlBox.Name = "layPanel_ControlBox";
            layPanel_ControlBox.BackColor = Color.White;
            layPanel_ControlBox.ImageToBackground(layPanel_ControlBox.Name);
            layPanel_ControlBox.Anchor = AnchorStyles.Top | AnchorStyles.Right;

            //mini button
            layBtn_Mini = new LayeredButton();
            layBtn_Mini.Name = "layBtn_Mini";
            //layBtn_Mini.NormalImage = TMUI.SkinImage(layBtn_Mini.Name);
            //layBtn_Mini.HoverImage = TMUI.SkinImage(layBtn_Mini.Name + "_Hover");
            //layBtn_Mini.PressedImage = TMUI.SkinImage(layBtn_Mini.Name + "_Pressed");
            layBtn_Mini.Text = "_";
            layBtn_Mini.Click += LayBtn_Mini_Click;

            //state switch button
            layBtn_StateSwitch = new LayeredButton();
            layBtn_StateSwitch.Name = "layBtn_StateSwitch";
            //layBtn_StateSwitch.NormalImage = TMUI.SkinImage(layBtn_StateSwitch.Name);
            //layBtn_StateSwitch.HoverImage = TMUI.SkinImage(layBtn_StateSwitch.Name + "_Hover");
            //layBtn_StateSwitch.PressedImage = TMUI.SkinImage(layBtn_StateSwitch.Name + "_Pressed");
            layBtn_StateSwitch.Text = "▣";
            layBtn_StateSwitch.Click += LayBtn_StateSwitch_Click;

            //close button
            layBtn_Close = new LayeredButton();
            layBtn_Close.Name = "layBtn_Close";
            //layBtn_Close.NormalImage = TMUI.SkinImage(layBtn_Close.Name);
            //layBtn_Close.HoverImage = TMUI.SkinImage(layBtn_Close.Name + "_Hover");
            //layBtn_Close.PressedImage = TMUI.SkinImage(layBtn_Close.Name + "_Pressed");
            layBtn_Close.Text = "×";
            layBtn_Close.Click += LayBtn_Close_Click;
        }

        private void LayBtn_Close_Click(object sender, EventArgs e)
        {
            Form frm = FindRoot(Parent);
            frm.Close();
        }

        private void LayBtn_StateSwitch_Click(object sender, EventArgs e)
        {
            Form frm = FindRoot(Parent);
            switch (frm.WindowState)
            {
                case FormWindowState.Normal:
                    frm.WindowState = FormWindowState.Maximized;
                    break;
                case FormWindowState.Minimized:
                    break;
                case FormWindowState.Maximized:
                    frm.WindowState = FormWindowState.Normal;
                    break;
                default:
                    break;
            }
        }

        private void LayBtn_Mini_Click(object sender, EventArgs e)
        {
            Form frm = FindRoot(Parent);
            frm.WindowState = FormWindowState.Minimized;
        }

        private Form FindRoot(Control ctl)
        {
            if (ctl.Parent == null)
            {
                return ctl as Form;
            }
            else
            {
                return FindRoot(ctl.Parent);
            }
        }

        private void UpdateLayout()
        {
            SetButtonLayout(layBtn_Mini, layBtn_StateSwitch, layBtn_Close);
            layPanel_ControlBox.Parent = Parent;
            layPanel_ControlBox.Location = new Point(Parent.Width - layPanel_ControlBox.Width, 0);
        }

        private void SetButtonLayout(params LayeredButton[] btn)
        {
            for (int i = 0; i < btn.Length; i++)
            {
                //btn[i].Parent = layPanel_ControlBox;
                btn[i].Dock = DockStyle.Right;
                btn[i].Size = buttonSize;
                btn[i].BaseColor = Color.Green;

                //btn[i].AdaptImage = true;
                //btn[i].BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
                //btn[i].ForeColor = Color.Green;
                //btn[i].Font = SystemFonts.DefaultFont;
                //btn[i].BackColor = System.Drawing.Color.Red;
                //btn[i].BaseColor = Color.Wheat;
                //btn[i].Borders.BottomColor = System.Drawing.Color.Empty;
                //btn[i].Borders.BottomWidth = 1;
                //btn[i].Borders.LeftColor = System.Drawing.Color.Empty;
                //btn[i].Borders.LeftWidth = 1;
                //btn[i].Borders.RightColor = System.Drawing.Color.Empty;
                //btn[i].Borders.RightWidth = 1;
                //btn[i].Borders.TopColor = System.Drawing.Color.Empty;
                //btn[i].Borders.TopWidth = 1;
                ////btn[i].Canvas = ((System.Drawing.Bitmap)(resources.GetObject("layeredButton1.Canvas")));
                //btn[i].ControlState = LayeredSkin.Controls.ControlStates.Normal;
                //btn[i].HaloColor = System.Drawing.Color.White;
                //btn[i].HaloSize = 5;
                //btn[i].HoverImage = null;
                //btn[i].IsPureColor = true;
                ////btn[i].Location = new System.Drawing.Point(156, 0);
                //btn[i].NormalImage = null;
                //btn[i].PressedImage = null;
                //btn[i].Radius = 0;
                //btn[i].ShowBorder = false;
                ////btn[i].Size = new System.Drawing.Size(32, 32);
                //btn[i].TabIndex = 0;
                ////btn[i].Text = "_";
                //btn[i].TextLocationOffset = new System.Drawing.Point(0, 0);
                //btn[i].TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
                //btn[i].TextShowMode = LayeredSkin.TextShowModes.Halo;
            }
        }
    }
}
